﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jungle.Rent;
using Jungle.Code.Core;
using Jungle.Code.Utils;


public partial class ShowOffer : System.Web.UI.UserControl
{
    SessionKeeper sk = new SessionKeeper();
    protected override void OnInit(EventArgs e)
    {
        if (Request["id"] != null)
        {
            Offer o = new Offer();
            o.OfferId = Convert.ToInt32(Request["id"]);
            o.GetById();

            switch (o.ObjectType)
            {
                case 1: RenderFlat(o); break;
                case 2: RenderComm(o); break;
                case 3: RenderHouse(o); break;
                case 4: RenderGarage(o); break;
                case 5: RenderGround(o); break;
            }
        }
        base.OnInit(e);
    }
    // ---------------------------------------------------------------------
    public void LoadImg(string files)
    {
        string[] ff = files.Split(',');
        string fn = "";
        if (ff.Length > 1)
        {
            img_Photo.ImageUrl = UrlHelper.GetSiteUrl() + ff[1];
            fn = ff[1];

            try
            {
                if (ff[2] != null)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[2]));
                    if (img.Width > 70) { img_2.Width = 70; img_2.Height = img.Height / (img.Width / 70); }
                    img_2.ImageUrl = UrlHelper.GetSiteUrl() + ff[2];
                    img_2.Visible = true;
                }
                if (ff[3] != null)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[3]));
                    if (img.Width > 70) { img_3.Width = 70; img_3.Height = img.Height / (img.Width / 70); }
                    img_3.ImageUrl = UrlHelper.GetSiteUrl() + ff[3];
                    img_3.Visible = true;
                }
                if (ff[4] != null)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[4]));
                    if (img.Width > 70) { img_4.Width = 70; img_4.Height = img.Height / (img.Width / 70); }
                    img_4.ImageUrl = UrlHelper.GetSiteUrl() + ff[4];
                    img_4.Visible = true;
                }
                if (ff[5] != null)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[5]));
                    if (img.Width > 70) { img_5.Width = 70; img_5.Height = img.Height / (img.Width / 70); }
                    img_5.ImageUrl = UrlHelper.GetSiteUrl() + ff[5];
                    img_5.Visible = true;
                }
                if (ff[6] != null)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(ff[6]));
                    if (img.Width > 70) { img_6.Width = 70; img_6.Height = img.Height / (img.Width / 70); }
                    img_6.ImageUrl = UrlHelper.GetSiteUrl() + ff[6];
                    img_6.Visible = true;
                }
            }
            catch { }
        }
        else
        {
            img_Photo.ImageUrl = UrlHelper.GetSiteUrl() + files;
            fn = files;
        }

        System.Drawing.Image img2 = System.Drawing.Image.FromFile(Server.MapPath(fn));
        if (img2.Width > 140) 
        { 
            img_Photo.Width = 140;

            double d1 = Math.Round((double)img2.Width / 140, 4);
            double d2 = Math.Round((double)img2.Height / d1, 4);
            int height = Convert.ToInt32(d2);

            if (height > 190)
            {

                img_Photo.Width = Convert.ToInt32(Math.Round(140.0 / (Math.Round((double)height / 190, 4))));
                height = 190;
            }

            img_Photo.Height = height;
        }

    }
    // ---------------------------------------------------------------------
    public void RenderFlat(Offer o)
    {
        OfferMenu.ActiveViewIndex = 0;
        lb_Info.Text = "<table class=\"tbl_ShowOffer\">";

        lb_Info.Text += "<tr><td class=\"left_td\">Код объекта</td><td class=\"td_ri\">" + Offer.GetObjectNomber(o.OfferId) + "</td></tr>";
        if (o.Photo.Length > 10)
        {
            LoadImg(o.Photo);
        }

        if (o.Komiss) lb_Komiss.Text = "Объект без комиссионных!";


        string a0 = "";
        if (o.OfferType) a0 = "<b>Продается:</b> "; else a0 = "<b>Сдается в аренду:</b> ";
        lb_OfferCode.Text += a0 + Offer.ddl_ObjectType().Items.FindByValue(o.ObjectType.ToString()).Text;

        if (o.Rooms > 0) lb_Info.Text += "<tr><td class=\"left_td\">Кол. комнат</td><td class=\"td_ri\">" + o.Rooms.ToString() + "</td></tr>";
        if (o.WallMaterials.Length > 0) lb_Info.Text += "<tr><td class=\"left_td\">Материал стен</td><td class=\"td_ri\">" + o.WallMaterials + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Оценка состояния</td><td class=\"td_ri\">" + Offer.GetQualityLevel(o.QualityLevel) + "</td></tr>";

        if (o.AnotherUsing.Length > 0) lb_Info.Text += "<tr><td class=\"left_td\">Возможное использование</td><td class=\"td_ri\">" + o.AnotherUsing + "</td></tr>";
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Адрес:</strong> <span style=\"color: #000;\">" + Offer.ddl_Region().Items.FindByValue(o.RegionId.ToString()).Text + ", " + o.City + ", " + o.Street + ", " + o.HomeNumber + "</span></div>";
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Цена: <span style=\"color: #000;\">" + o.Price + " " + GetMoney(o.MoneyType);
        if (!o.OfferType)
        {
            if (o.ArendType == "длительно") lb_OfferCode.Text += " в месяц</span></div>";
            if (o.ArendType == "посуточно") lb_OfferCode.Text += " в сутки</span></div>";
        }

        if (o.Floors != 0 || o.Floor != 0)
        {
            lb_Info.Text += "<tr><td class=\"left_td\">";
            if (o.Floor > 0 && o.Floors > 0) lb_Info.Text += "Этаж/Этажность</td><td class=\"td_ri\">" + o.Floor + "/" + o.Floors + "</td></tr>";
            if (o.Floor > 0 && o.Floors == 0) lb_Info.Text += "Этаж</td><td class=\"td_ri\">" + o.Floor + "</td></tr>";
            if (o.Floor == 0 && o.Floors > 0) lb_Info.Text += "Этажность</td><td class=\"td_ri\">" + o.Floors + "</td></tr>";
        }


        lb_Info.Text += "<tr><td class=\"left_td\">Площадь м<sup>2</sup> (общая/жилая/кухня)</td><td class=\"td_ri\">" + o.AreaTotal + "/" + o.AreaLive + "/" + o.AreaKitchen + "</td></tr>";
        if (o.Badroom) lb_Info.Text += "<tr><td class=\"left_td\">Санузел</td><td class=\"td_ri\">совмещенный</td></tr>"; else lb_Info.Text += "<tr><td class=\"left_td\">Санузел</td><td class=\"td_ri\">раздельный</td></tr>";
        if (o.Balcon > 0) lb_Info.Text += "<tr><td class=\"left_td\">Балконы</td><td class=\"td_ri\">" + o.Balcon + "</td></tr>";
        lb_Info.Text += "</td></tr>";
        lb_Info.Text += "</table>";
        lb_Info.Text += "<div class=\"descript_block\"><strong>Описание:</strong> " + o.Description.Replace("\r\n", "<br/>") +"</div>";

        lb_Info.Text += GetAutorInfo(o);

        

    }
    // ---------------------------------------------------------------------
    public void RenderComm(Offer o)
    {
        OfferMenu.ActiveViewIndex = 0;
        lb_Info.Text = "<table class=\"tbl_ShowOffer\">";

        lb_Info.Text += "<tr><td class=\"left_td\">Код объекта</td><td class=\"td_ri\">" + Offer.GetObjectNomber(o.OfferId) + "</td></tr>";
        if (o.Photo.Length > 10)
        {
            LoadImg(o.Photo);
        }
        string a0 = "";
        if (o.OfferType) a0 = "<b>Продается:</b> "; else a0 = "<b>Сдается в аренду:</b> ";
        lb_OfferCode.Text += a0 + Offer.ddl_UnderType(o.ObjectType).Items.FindByValue(o.UnderTypeId.ToString()).Text;
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Адрес:</strong> <span style=\"color: #000;\">" + Offer.ddl_Region().Items.FindByValue(o.RegionId.ToString()).Text + ", " + o.City + ", " + o.Street + ", " + o.HomeNumber + "</span></div>";
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Цена: <span style=\"color: #000;\">" + o.Price + " " + GetMoney(o.MoneyType) + " " + o.PayBy;
        if (!o.OfferType)
        {
            if (o.ArendType == "длительно") lb_OfferCode.Text += " в месяц</span></div>";
            if (o.ArendType == "посуточно") lb_OfferCode.Text += " в сутки</span></div>";
        }

        if (o.Komiss) lb_Komiss.Text = "Объект без комиссионных!";

        lb_Info.Text += "<tr><td class=\"left_td\">Оценка состояния</td><td class=\"td_ri\">" + Offer.GetQualityLevel(o.QualityLevel) + "</td></tr>";
        if (o.NowUsing.Length > 0) lb_Info.Text += "<tr><td class=\"left_td\">Текущее использование</td><td class=\"td_ri\">" + o.NowUsing + "</td></tr>";
        if (o.AnotherUsing.Length > 0) lb_Info.Text += "<tr><td class=\"left_td\">Возможное использование</td><td class=\"td_ri\">" + o.AnotherUsing + "</td></tr>";

        if (o.Floors != 0 || o.Floor != 0)
        {
            lb_Info.Text += "<tr><td class=\"left_td\">";
            if (o.Floor > 0 && o.Floors > 0) lb_Info.Text += "Этаж/Этажность</td><td class=\"td_ri\">" + o.Floor + "/" + o.Floors + "</td></tr>";
            if (o.Floor > 0 && o.Floors == 0) lb_Info.Text += "Этаж</td><td class=\"td_ri\">" + o.Floor + "</td></tr>";
            if (o.Floor == 0 && o.Floors > 0) lb_Info.Text += "Этажность</td><td class=\"td_ri\">" + o.Floors + "</td></tr>";
        }
        lb_Info.Text += "<tr><td class=\"left_td\">Площадь м<sup>2</sup> (общая)</td><td class=\"td_ri\">" + o.AreaTotal + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Площадь участка</sup></td><td class=\"td_ri\">" + o.FieldArea + GetAreaType(o.FieldAreaType) + "</td></tr>";
        lb_Info.Text += "</table>";
        lb_Info.Text += "<div class=\"descript_block\"><strong>Описание:</strong> " + o.Description.Replace("\r\n", "<br/>") +"</div>";


        lb_Info.Text += GetAutorInfo(o);

    }
    // ---------------------------------------------------------------------
    public void RenderHouse(Offer o)
    {
        OfferMenu.ActiveViewIndex = 0;

        lb_Info.Text = "<table class=\"tbl_ShowOffer\">";

        lb_Info.Text += "<tr><td class=\"left_td\">Код объекта</td><td class=\"td_ri\">" + Offer.GetObjectNomber(o.OfferId) + "</td></tr>";
        if (o.Photo.Length > 10)
        {
            LoadImg(o.Photo);
        }
        string a0 = "";
        if (o.OfferType) a0 = "<b>Продается:</b> "; else a0 = "<b>Сдается в аренду:</b> ";
        lb_OfferCode.Text += a0 + Offer.ddl_UnderType(o.ObjectType).Items.FindByValue(o.UnderTypeId.ToString()).Text;
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Адрес:</strong> <span style=\"color: #000;\">" + Offer.ddl_Region().Items.FindByValue(o.RegionId.ToString()).Text + ", " + o.City + ", " + o.Street + ", " + o.HomeNumber + "</span></div>";
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Цена: <span style=\"color: #000;\">" + o.Price + " " + GetMoney(o.MoneyType);
        if (!o.OfferType)
        {
            if (o.ArendType == "длительно") lb_OfferCode.Text += " в месяц</span></div>";
            if (o.ArendType == "посуточно") lb_OfferCode.Text += " в сутки</span></div>";
        }

        if (o.Komiss) lb_Komiss.Text = "Объект без комиссионных!";

        if (o.Rooms > 0) lb_Info.Text += "<tr><td class=\"left_td\">Кол. комнат</td><td class=\"td_ri\">" + o.Rooms.ToString() + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Оценка состояния</td><td class=\"td_ri\">" + Offer.GetQualityLevel(o.QualityLevel) + "</td></tr>";
        if (o.WallMaterials.Length > 0) lb_Info.Text += "<tr><td class=\"left_td\">Материал стен</td><td class=\"td_ri\">" + o.WallMaterials + "</td></tr>";
        if (o.PerMaterials.Length > 0) lb_Info.Text += "<tr><td class=\"left_td\">Материал перекрытий</td><td class=\"td_ri\">" + o.PerMaterials + "</td></tr>";
        if (o.Floors != 0) lb_Info.Text += "<tr><td class=\"left_td\">Этажность</td><td class=\"td_ri\">" + o.Floors + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Год постройки</td><td class=\"td_ri\">" + o.YearBuild.ToString() + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Степень готовности</td><td class=\"td_ri\">" + o.LevelDone + "%" + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Расположение</td><td class=\"td_ri\">" + o.Place + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Площадь м<sup>2</sup> (общая/жилая/кухня)</td><td class=\"td_ri\">" + o.AreaTotal + "/" + o.AreaLive + "/" + o.AreaKitchen + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Площадь участка</sup></td><td class=\"td_ri\">" + o.FieldArea + " " + GetAreaType(o.FieldAreaType) + "</td></tr>";

        lb_Info.Text += "</table>";
        lb_Info.Text += "<div class=\"descript_block\"><strong>Описание:</strong> " + o.Description.Replace("\r\n", "<br/>") +"</div>";

        lb_Info.Text += GetAutorInfo(o);

    }
    // ---------------------------------------------------------------------
    public void RenderGarage(Offer o)
    {
        OfferMenu.ActiveViewIndex = 0;

        lb_Info.Text = "<table class=\"tbl_ShowOffer\">";

        lb_Info.Text += "<tr><td class=\"left_td\">Код объекта</td><td class=\"td_ri\">" + Offer.GetObjectNomber(o.OfferId) + "</td></tr>";
        if (o.Photo.Length > 10)
        {
            LoadImg(o.Photo);
        }
        string a0 = "";
        if (o.OfferType) a0 = "<b>Продается:</b> "; else a0 = "<b>Сдается в аренду:</b> ";
        lb_OfferCode.Text += a0 + Offer.ddl_UnderType(o.ObjectType).Items.FindByValue(o.UnderTypeId.ToString()).Text;
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Адрес:</strong> <span style=\"color: #000;\">" + Offer.ddl_Region().Items.FindByValue(o.RegionId.ToString()).Text + ", " + o.City + ", " + o.Street + ", " + o.HomeNumber + "</span></div>";
        lb_OfferCode.Text += "<div style=\" font-weight: normal; color: #aaa; \">Цена: <span style=\"color: #000;\">" + o.Price + " " + GetMoney(o.MoneyType);
        if (!o.OfferType)
        {
            if (o.ArendType == "длительно") lb_OfferCode.Text += " в месяц</span></div>";
            if (o.ArendType == "посуточно") lb_OfferCode.Text += " в сутки</span></div>";
        }

        if (o.Komiss) lb_Komiss.Text = "Объект без комиссионных!";

        lb_Info.Text += "<tr><td class=\"left_td\">Оценка состояния</td><td class=\"td_ri\">" + Offer.GetQualityLevel(o.QualityLevel) + "</td></tr>";
        if (o.WallMaterials.Length > 0) lb_Info.Text += "<tr><td class=\"left_td\">Материал стен</td><td class=\"td_ri\">" + o.WallMaterials + "</td></tr>";
        if (o.Floors != 0) lb_Info.Text += "<tr><td class=\"left_td\">Этажность</td><td class=\"td_ri\">" + o.Floors + "</td></tr>" + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Год постройки</td><td class=\"td_ri\">" + o.YearBuild.ToString() + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Степень готовности</td><td class=\"td_ri\">" + o.LevelDone + "%" + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Статус</td><td class=\"td_ri\">" + o.Status + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Площадь м<sup>2</sup> (общая)</td><td class=\"td_ri\">" + o.AreaTotal + "</td></tr>";
        lb_Info.Text += "</table>";
        lb_Info.Text += "<div class=\"descript_block\"><strong>Описание:</strong> " + o.Description.Replace("\r\n", "<br/>") +"</div>";

        lb_Info.Text += GetAutorInfo(o);

    }
    // ---------------------------------------------------------------------
    public void RenderGround(Offer o)
    {
        OfferMenu.ActiveViewIndex = 0;

        lb_Info.Text = "<table class=\"tbl_ShowOffer\">";

        lb_Info.Text += "<tr><td class=\"left_td\">Код объекта</td><td class=\"td_ri\">" + Offer.GetObjectNomber(o.OfferId) + "</td></tr>";
        if (o.Photo.Length > 10)
        {
            LoadImg(o.Photo);
        }
        string a0 = "";
        if (o.OfferType) a0 = "<b>Продается</b> "; else a0 = "<b>Сдается в аренду</b> ";
        lb_OfferCode.Text += a0 + " участок земли <div style=\" font-weight: normal; color: #aaa; \">Цена: <span style=\"color: #000;\">" + o.Price + " " + GetMoney(o.MoneyType) + " за " + o.PayBy;

        if (o.Komiss) lb_Komiss.Text = "Объект без комиссионных!";
        lb_Info.Text += "<tr><td class=\"left_td\">Оценка состояния</td><td class=\"td_ri\">" + Offer.GetQualityLevel(o.QualityLevel) + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Расположение</td><td class=\"td_ri\">" + o.Place + "</td></tr>";
        lb_Info.Text += "<tr><td class=\"left_td\">Площадь участка</sup></td><td class=\"td_ri\">" + o.FieldArea + " " + GetAreaType(o.FieldAreaType) + "</td></tr>";
        lb_Info.Text += "</table>";
        lb_Info.Text += "<div class=\"descript_block\"><strong>Описание:</strong> " + o.Description.Replace("\r\n", "<br/>") +"</div>";

        lb_Info.Text += GetAutorInfo(o);



    }
    // ---------------------------------------------------------------------
    private string GetMoney(int id)
    {
        string result = "";
        switch (id)
        {
            case 1: result = "USD"; break;
            case 2: result = "EUR"; break;
            case 3: result = "UAH"; break;
        }

        return result;
    }
    // ---------------------------------------------------------------------
    private string GetAreaType(int id)
    {
        string result = "";
        switch (id)
        {
            case 1: result = "м2"; break;
            case 2: result = "с"; break;
            case 3: result = "га"; break;
        }

        return result;
    }
    // ---------------------------------------------------------------------
    private string GetAutorInfo(Offer o){
        string result = "";

        Jungle.Users.UserManager us = new Jungle.Users.UserManager();
        us.Email = o.Email; us.GetByEmail();
        int h = 0;
        int w = 70;
        try
        {
            System.Drawing.Image photo = System.Drawing.Image.FromFile(Server.MapPath(us.Photo));
            if (photo.Width < w) w = photo.Width;
            double d1 = Math.Round((double)photo.Width / w, 4);
            double d2 = Math.Round((double)photo.Height / d1, 4);
            h = Convert.ToInt32(d2);
        }
        catch { }



        result += "<table class=\"tbl_Autor\">";
        result += "<tr><td style=\"width: 100px;\">";
        try { if (us.Photo.Length > 0) result += "<img onclick=\"Show(this);\" src=\"" + UrlHelper.GetSiteUrl() + us.Photo + "\" style=\"width:" + w.ToString() + "; height:" + h.ToString() + "; cursor: pointer;\">"; }
        catch { }
        result += "</td><td>";
        result += "<strong>Ваш  специалист по данному объекту: </strong><br/>";
        result += o.MPhone + " " + o.Name + " " + o.HPhone;
        result += "<br/>Телефоны: " + o.WPhone;
        if(Request.RawUrl.IndexOf("admin") != -1)result += "<br/>История контактов: " + o.Fax;
        result += "<br/>Email: <a href=mailto:" + o.Email + ">" + o.Email + "</a>";
        result += "</td></tr>";
        result += "</table>";

        return result;

    }
    

}
